//
//  DataStatItemCell.swift
//  SitPosition
//
//  Created by apple on 2019/1/27.
//  Copyright © 2019 apple. All rights reserved.
//

import Foundation
import UIKit
import Cartography


class DataStatItemCell : UITableViewCell, Reuseable {
    
    // MARK: - Properties
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = Theme.Color.primaryDarkTextColor
        label.font = Theme.Font.regularFont(ofSize: 16)
        label.numberOfLines = 1
        label.lineBreakMode = NSLineBreakMode.byTruncatingTail
        return label
    }()
    
    private lazy var durationLabel: UILabel = {
        let label = UILabel()
        label.textColor = Theme.Color.primaryDarkTextColor
        label.font = Theme.Font.regularFont(ofSize: 16)
        label.numberOfLines = 1
        label.lineBreakMode = NSLineBreakMode.byTruncatingTail
        return label
    }()
    
    private lazy var percentLabel: UILabel = {
        let label = UILabel()
        label.textColor = Theme.Color.primaryDarkTextColor
        label.font = Theme.Font.regularFont(ofSize: 16)
        label.numberOfLines = 1
        label.lineBreakMode = NSLineBreakMode.byTruncatingTail
        label.textAlignment = .right
        return label
    }()
    
    // MARK: - Object lifecycle
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reuseIdentifier)
        contentView.addSubviews([
            titleLabel,
            durationLabel,
            percentLabel
            ])
        _setupContraints()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 数据模型
    var data: DataStatItem? = nil {
        didSet {
            titleLabel.text = data?.type.description
            durationLabel.text = data?.durationDisplayString
            percentLabel.text = data?.percentDisplayString
        }
    }
}


// MARK: - Private Methods

extension DataStatItemCell {
    
    // 设置视图约束
    func _setupContraints() {
        // titleLabel
        constrain(titleLabel, contentView) { v, parent in
            v.centerY == parent.centerY
            v.left == parent.left + 35
            v.width == parent.width * 2.0/5.0
            v.height == 19
        }
        
        // durationLabel
        constrain(durationLabel, titleLabel, contentView) { v, sibling, parent in
            v.centerY == parent.centerY
            v.left == sibling.right
            v.width == 80
            v.height == sibling.height
        }
        
        // percentLabel
        constrain(percentLabel, durationLabel, contentView) { v, sibling, parent in
            v.centerY == parent.centerY
            v.left == sibling.right
            v.right == parent.right - 35
            v.height == sibling.height
        }
    }
}
